package leetcode.editor.cn;

//Java：螺旋矩阵 II
public class SpiralMatrixIi {
    public static void main(String[] args) {
        Solution solution = new SpiralMatrixIi().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * 这个过程就是模拟填充四条边的过程，可以用x,y坐标进行辅助
     */
    class Solution {
        public int[][] generateMatrix(int n) {
            int[][] nums = new int[n][n];
            int loop = 0;
            int startY = 0;
            int startX = 0;
            int offset = 1;
            int count = 1;
            int x, y;
            while (loop++ < n / 2) {
                for (x = startX; x < n - offset; x++) {
                    nums[startY][x] = count++;
                }

                for (y = startY; y < n - offset; y++) {
                    nums[y][x] = count++;
                }

                for (; x > startX; x--) {
                    nums[y][x] = count++;
                }

                for (; y > startY; y--) {
                    nums[y][x] = count++;
                }
                startX++;
                startY++;
                offset++;
            }
            if (n % 2 == 1) {
                nums[startY][startX] = count;
            }
            return nums;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}